# -*- coding:utf-8 -*-
import os,sys
import re
import traceback
import time
sys.path.append(os.path.join(os.path.abspath(os.path.dirname(__file__)), os.pardir))
import supeanut_config
from CommonLib.mylog import mylog
from CommonLib.FileTool.CsvTool import CsvTool
from CommonLib.StockTool import StockTool


'''
作者：supeanut
创建时间：2016-xx-xx xx:xx:xx
	功能描述：
	一次性工具
	剔除债券ETF等基金数据
	规范化检查，过滤 log
	转成day标准格式，
		包括字段顺序，date_time转成date， code规范（in、sz、sh）,去除头部field描述,vol_stock*100
	以股票代码为文件,不再分SH,SZ文件夹
相关配置：
	supeanut_config.XXX
历史改动：
	2016-xx-xx: xxxxxx
'''
class WsDayDataSupport:
	def __init__(self):
		self.csvtool = CsvTool()
		self.stocktool = StockTool()
		self.ori_path = supeanut_config.WS_ORI_DAYDATA_CSV_PATH
		self.field = supeanut_config.WS_ORI_DAYDATA_CSV_FIELD
		self.des_path = supeanut_config.WS_DES_DAYDATA_CSV_PATH
		# 子目录
		self.SHSZ_path = ['SZ', 'SH']
		# 用于数据格式验证的RE
		self.value_RE = re.compile(r'^[0-9]+(\.[0-9]*)?$')
		self.datetime_RE = re.compile(r'^[0-9]{4}-[01][0-9]-[0123][0-9] [012][0-9]:[0-5][0-9]:[0-5][0-9]$')


	# 抽取所有需要用到的股票代码文件的路径和文件名
	# OUTOUT: [['PATH','FILE','STANDARD CODE], ...]
	def extract_path(self, ):
		result_pairs = []
		flag, stand_codes = self.stocktool.get_all_stand_codes()
		if flag is False:
			return False, stand_codes
		for SHSZ in self.SHSZ_path:
			SHSZ_dir = os.path.join(self.ori_path, SHSZ)
			# 遍历文件
			csv_files = os.listdir(SHSZ_dir)
			for csv_file in csv_files:
				wscode = csv_file.split(r'.')[0]
				stand_code = self.stocktool.ws_code2stand_code(wscode)
				if stand_code not in stand_codes:
					continue
				result_pairs.append([SHSZ_dir, csv_file, stand_code])
		return True, result_pairs

	# 提取一个股票文件的数据，并验证
	def verify_one_stock(self, path_info):
		log = mylog('DataSupport.WsDayDataSupport.verify', None)
		path = path_info[0]
		file_name = path_info[1]
		standard_code = path_info[2]
		# csv列数一致性验证
		flag, msg = self.csvtool.verify_csv(path, file_name, colume_num =len(self.field)) 
		if flag is False:
			return False, msg
		# 读取csv数据
		flag, msg = self.csvtool.read_csv(path, file_name)
		if flag is False:
			return False, msg
		# 去除第一行描述性字段
		records = msg[1:]
		# 数据格式验证
		verified_records = []
		for record in records:
			# 配置文件相关！
			date_time = record[0]
			values = record[1:]
			a = self.datetime_RE.match(date_time)
			if a is None:
				log.warning('date_time verify faild:%s,code:%s'%(date_time,standard_code))
				# 略过错误行
				continue
			value_tag = True
			for value in values:
				b = self.value_RE.match(value)
				if b is None:
					log.warning('value verify faild:%s,code:%s'%(value,standard_code))
					value_tag = False
					break
			if value_tag is False:
				# 略过错误行
				continue
			verified_records.append(record)
		return True, [verified_records, standard_code]
	
	# 转换列顺序，转换datetime-->date
	def trans_one_stock(self, record_code):
		records = record_code[0]
		sd_code = record_code[1]
		new_records = []
		for record in records:
			# 与配置文件相关！
			new_record = []
			new_record.append(sd_code)
			date = record[0][:10]
			new_record.append(date)
			new_record.append(record[1])
			new_record.append(record[3])
			new_record.append(record[2])
			for index in [4,5,6]:
				new_record.append(record[index])
			new_records.append(new_record)
		return new_records, sd_code

	def main_process(self, ):
		flag, msg = self.extract_path()
		if flag is False:
			return False, msg
		for path_info in msg:
			flag, msg2 = self.verify_one_stock(path_info)
			if flag is False:
				return False, msg2
			records, code = self.trans_one_stock(msg2)
			flag, msg4 = self.store(records, code)
			if flag is False:
				return False, msg4
		return True, 'suc'
		
		
	# 存到本地csv
	def store(self, day_data, code):
		log = mylog('DataSupport.WsDayDataSupport.store', None)
		# 用到配置文件信息，注意同步
		# 以code为index
		flag, msg = self.csvtool.update_csvfile([1], day_data, self.des_path, code)
		if flag is False:
			return False, msg
		log.info('store stocks num:%d'%len(day_data))
		return True, 'suc'

if __name__ == '__main__':
	obj = WsDayDataSupport()
	print obj.main_process()
